#!/bin/bash

echo "========================================"
echo "正在构建KWDB导入工具..."
echo "========================================"

# 检查Go环境
if ! command -v go &> /dev/null; then
    echo "错误: 未找到Go环境，请先安装Go 1.18或更高版本"
    exit 1
fi

# 显示Go版本
echo "Go环境检查:"
go version
echo

# 清理模块缓存
echo "清理模块缓存..."
go clean -modcache

# 下载依赖
echo "下载依赖包..."
go mod tidy
if [ $? -ne 0 ]; then
    echo "错误: 下载依赖包失败"
    exit 1
fi

# 构建程序
echo "构建可执行文件..."
go build -o kwdb-import ./cmd/kwdb-import
if [ $? -ne 0 ]; then
    echo "错误: 构建失败"
    exit 1
fi

# 设置执行权限
chmod +x kwdb-import

echo
echo "========================================"
echo "构建完成！"
echo "========================================"
echo
echo "可执行文件: kwdb-import"
echo "使用方法:1"
echo "  ./kwdb-import ./examples/sample.sql -c host:port/database -u username -p password "
echo "使用方法2:"
echo "  ./kwdb-import  --config ./config.example.yaml 
echo
echo "查看帮助:"
echo "  ./kwdb-import --help"
echo
echo "查看版本:"
echo "  ./kwdb-import version"
echo